package cq.zhcqk.widget;

import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.widget.TextView;

import cq.zhcqk.R;


/**
 * 获取短信验证码倒计时
 * @author le
 *
 */
@SuppressLint("ResourceAsColor")
public class RegisterCodeTimer extends CountDownTimer{
	public static final int TIME_COUNT = 121000;//时间，从60秒开始
	private TextView btn;
	private int endStrRid;
	private int normalColor;//未计时文字颜色
	private int timingColor;//计时文字颜色
	/**
	 * @param millisInFuture
	 * @param countDownInterval
	 * @param btn 点击的按钮
	 * @param endStrRid 倒计时后按钮显示的文字
	 */
	public RegisterCodeTimer(long millisInFuture, long countDownInterval, TextView btn, int endStrRid) {
		super(millisInFuture, countDownInterval);
		this.endStrRid = endStrRid;
		this.btn = btn;
	}

	public RegisterCodeTimer(TextView btn, int endStrRid, int normalColor, int timingColor){
		super(TIME_COUNT, 1000);
		this.endStrRid = endStrRid;
		this.btn = btn;
	}

	public RegisterCodeTimer(TextView btn){
		super(TIME_COUNT, 1000);
		this.endStrRid = R.string.register_getMsgCode_validate;
		this.btn = btn;
	}
	/**
	 * 计时完毕触发
	 */
	@Override
	public void onFinish() {
		if(normalColor > 0){
			btn.setTextColor(normalColor);
			}
		//btn.setBackgroundColor(R.color.login_register_button_bg);
		btn.setText(endStrRid);
		btn.setEnabled(true);
	}
	/**
	 * 计时过程
	 */
	@Override
	public void onTick(long millisUntilFinished) {
		if(timingColor > 0){
			btn.setTextColor(timingColor);
		}
		//btn.setBackgroundColor(R.color.login_register_button_click_bg);
		btn.setEnabled(false);
		btn.setText(millisUntilFinished / 1000+"S");
	}

}
